package com.five.welcome.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.five.welcome.entity.WelcomeAnnouncement;
import com.five.welcome.dao.WelcomeAnnouncementMapper;
import com.five.welcome.service.WelcomeAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ywt
 * @since 2024-07-15
 */
@Service
public class WelcomeAnnouncementServiceImpl extends ServiceImpl<WelcomeAnnouncementMapper, WelcomeAnnouncement> implements WelcomeAnnouncementService {

    @Autowired
    private WelcomeAnnouncementMapper welcomeAnnouncementMapper;

    //查询所有公告
    @Override
    public List<WelcomeAnnouncement> getALlAnnounce() {
        QueryWrapper<WelcomeAnnouncement> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        return welcomeAnnouncementMapper.selectList(queryWrapper);
    }

    //添加公告
    @Override
    public Integer addAnnounce(WelcomeAnnouncement welcomeAnnouncement) {
        welcomeAnnouncement.setPublishTime(getCurrentDateString());
        welcomeAnnouncement.setUpdateTime(getCurrentDateString());
        return welcomeAnnouncementMapper.insert(welcomeAnnouncement);
    }

    //删除公告
    @Override
    public int deleteAnnounceById(Integer id) {
        return welcomeAnnouncementMapper.deleteAnnounceById(id);
    }

    //修改公告
    @Override
    public int updateAnnounceById(WelcomeAnnouncement welcomeAnnouncement) {
        UpdateWrapper<WelcomeAnnouncement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(welcomeAnnouncement.getTitle() != null, "title", welcomeAnnouncement.getTitle())
                .set(welcomeAnnouncement.getContent() != null, "content", welcomeAnnouncement.getContent())
                .set(welcomeAnnouncement.getAuthor() != null, "author", welcomeAnnouncement.getAuthor())
                .set("update_time", getCurrentDateString())
                .eq("announcement_id", welcomeAnnouncement.getAnnouncementId());
        return welcomeAnnouncementMapper.update(updateWrapper);
    }

    //审核公告
    @Override
    public int updateAnnounceStatusById(Integer announcementStatus, Integer announcementId) {
        UpdateWrapper<WelcomeAnnouncement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("announcement_status", announcementStatus)
                .set("update_time", getCurrentDateString())
                .eq("announcement_id", announcementId);
        return welcomeAnnouncementMapper.update(updateWrapper);
    }

    //获取首页公告
    @Override
    public WelcomeAnnouncement getAnnounceOnStart() {
        QueryWrapper<WelcomeAnnouncement> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*")
                .eq("onstart", 1);
        return welcomeAnnouncementMapper.selectOne(queryWrapper);
    }

    //修改状态
    @Override
    public void modOnStartById(Integer id, Integer onStart) {
        if (onStart == 1) {
            //将传过来的改成显示在首页
            welcomeAnnouncementMapper.modOnStartById(id, getCurrentDateString());
            //将其他改成不显示在首页
            welcomeAnnouncementMapper.modUnStartById(id, getCurrentDateString());
        } else if (onStart == 0) {
            UpdateWrapper<WelcomeAnnouncement> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("onstart", 0)
                    .set("update_time", getCurrentDateString())
                    .eq("announcement_id", id);
            welcomeAnnouncementMapper.update(updateWrapper);
        }
    }

    //获取当前时间字符串
    public static String getCurrentDateString() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.now().format(dateTimeFormatter);
    }
}
